package com.beiye.drivertransport.utils;

import android.content.Context;
import android.view.View;
import android.widget.Toast;

public class ToastUtil {
    /**
     * 避免重复Toast内容显示
     */
    private static ToastUtil instance=null;

    private static Toast mToast;
    private static Toast sToast;
    private static Context mContext;

    public static ToastUtil getInstance(){
        if(instance==null){
            instance=new ToastUtil();
        }
        return instance;
    }

    //为了不让Toast重复显示这里用了一个类变量来监听Toast是否在显示
    public static void showShortToast(Context context, String text) {
        if(mToast == null) {
            mToast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(text);
            mToast.setDuration(Toast.LENGTH_LONG);
        }
        mToast.show();
    }

    //取消方法则是需要在onBackPressed方法中调用,否则会在其他activity中显示
    public void cancelToast() {
        if (mToast != null) {
            mToast.cancel();
        }
    }
   //解决小米手机弹toast带应用名
    //传入要显示的内容和显示时间，getContext()是Utils类里面定义的方法，就是你要使用的上下文环境
    public static void updateToast(String res, int duration) {
        if (sToast == null) {
            View v = Toast.makeText(mContext, "", Toast.LENGTH_SHORT).getView();
            sToast = new Toast(mContext);
            sToast.setView(v);
        }
        sToast.setText(res);
        sToast.setDuration(duration);
        sToast.show();
    }

}
